const app = getApp();
Page({
  data: {
    avatarUrl: '/image/user/avatar.png',
    username: '点击登录',
    hasUserInfo: false,
    mobile: '',
    list: [
      { title: '联系客服', icon: 'manager-o' },
      { title: '分享素材', icon: 'photo-o' },
      { title: '立即分享', icon: 'pending-payment' },
      { title: '投诉反馈', icon: 'paid', url: "/pages/subPages/complain/complain" },
      { title: '用户协议', icon: 'description-o', url: "/pages/subPages/userAgreement/userAgreement" },
      { title: '隐私协议', icon: 'shield-o', url: "/pages/subPages/privacyAgreement/privacyAgreement" },
      { title: '全部订单', icon: 'shield-o', url: "/pages/subPages/allOrder/allOrder" },
    ],
    userInfo: {},

    //弹窗
    dialogShow: false,
    beforeClose: {},
    appid: wx.getAccountInfoSync().miniProgram.appId,
    dialogConfirm: 0 //为0的时候不能关闭弹窗
  },
  onShareAppMessage(res) {
    return {
      title: '微票电影',
      path: 'pages/home/home'
    }
  },
  onLoad: function () {
    var that = this
    that.setData({
      beforeClose: (action) => this.onBeforeClose(action)
    })
  },
  onShow: function () {
    this.userAuthorized();
  },

  // 实时获取 身份信息和个人数据（防止出现数据不对等的情况）
  userAuthorized() {
    var _this = this;
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        userInfo
      })
      this.getUserMessage(userInfo.consumerId)
    }
  },
  // 登录
  onGetUserInfo(event) {
    var that = this;
    const userInfo = event.detail.userInfo
    if (userInfo) {
      wx.login({
        success: function (res_1) {
          wx.getUserInfo({
            success: function (res_2) {
              console.log(res_2)
              wx.request({
                url: app.globalData.url + '/app/wx/miniProgramLogin',
                method: 'POST',
                header: {
                  'Accept': 'application/json'
                },
                data: {
                  // code: "loginCode",
                  code: res_1.code,
                  recommendCode: app.globalData.recommendCode,
                  nickName: res_2.userInfo.nickName,
                  avatarUrl: res_2.userInfo.avatarUrl,
                  gender: res_2.userInfo.gender,
                  appid: wx.getAccountInfoSync().miniProgram.appId
                },
                success: function (res) {
                  console.log(res.data.data);
                  that.getUserMessage(res.data.data.consumerId)
                }
              })
            }
          })
        }
      })
    }
  },
  // 获取用户信息
  getUserMessage(consumerId) {
    var _this = this;
    wx.request({
      url: app.globalData.url + '/app/operate/getUserMessage',
      method: 'POST',
      header: {
        'Accept': 'application/json'
      },
      data: {
        consumerId,
      },
      success: function (res1) {
        const userInfo = res1.data.data
        wx.setStorage({
          key: 'userInfo',
          data: userInfo
        })
        _this.setData({
          hasUserInfo: true,
          userInfo
        })
        app.globalData.userInfo = userInfo
      }
    })
  },
  toDetail(e) {
    const amount = this.data.userInfo.amount
    const type = e.currentTarget.dataset.type;
    const url = e.currentTarget.dataset.url;
    if (!url) return
    if (type == 'filmOrder') {
      wx.switchTab({
        url,
      })
    } else if (type == 'withdrawalCenter') {
      wx.navigateTo({
        url: `${url}?commission=${amount}`
      })
    } else if (type == 'draft') {
      wx.navigateTo({
        url: `${url}?currentTab=${1}`
      })
    } else if (type == 'allOrder') {
      wx.navigateTo({
        url: `${url}?currentTab=${0}`
      })
    } else {
      wx.navigateTo({
        url,
      })
    }
  },
  onShareAppMessage(res) {
    console.log(res);
    return {
      title: '热门大片特价优惠购票立减5-20元，赶快来吧！',
      path: `pages/home1/index?recommendCode=${this.data.userInfo.consumerId}`,
      imageUrl: 'https://gzcy-1318962991.cos.ap-shanghai.myqcloud.com/jpg/1708262732151.jpg'   //  分享的图片地址
    }
  },
  // 立即赚钱
  upDialog() {
    const _this = this;
    if (this.data.userInfo.mobileNumber) {
      wx.request({
        url: app.globalData.url + '/app/wx/registerSupplier',
        method: 'POST',
        header: {
          'Accept': 'application/json'
        },
        data: {
          consumerId: this.data.userInfo.consumerId,
          mobileNumber: this.data.userInfo.mobileNumber
        },
        success: function (res1) {
          if (res1.data.code == 200) {
            wx.showToast({
              icon: 'success',
              title: '注册成功',
            })
            _this.getUserMessage(_this.data.userInfo.consumerId)
          } else {
            wx.showToast({
              icon: 'none',
              title: res1.data.msg,
            })
          }
        }
      })
    } else {
      this.setData({ dialogShow: true })
    }
  },
  // 取消注册商户
  onClose() {
    this.setData({ dialogShow: false, mobile: '' })
  },
  // 注册商户
  onConfirm() {
    const _this = this;
    if (!this.data.mobile) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号码',
      })
      this.setData({
        dialogConfirm: 0
      })
    }
    if (this.data.mobile) {
      var pattern = /^1[3456789]\d{9}$/;
      if (!pattern.test(this.data.mobile)) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号码',
        })
        return false
      }
    }
    wx.request({
      url: app.globalData.url + '/app/wx/registerSupplier',
      method: 'POST',
      header: {
        'Accept': 'application/json'
      },
      data: {
        consumerId: this.data.userInfo.consumerId,
        mobileNumber: this.data.mobile
      },
      success: function (res1) {
        if (res1.data.code == 200) {
          wx.showToast({
            icon: 'success',
            title: '注册成功',
          })
          _this.setData({
            dialogShow: false,
          })
          _this.getUserMessage(_this.data.userInfo.consumerId)
        } else {
          _this.setData({
            dialogShow: false,
          })
          wx.showToast({
            icon: 'none',
            title: res1.data.msg,
          })
        }

      }
    })
  },
  // 注册商户
  onBeforeClose: function (action) {
    const that = this;
    return new Promise((resolve) => {
      setTimeout(() => {
        var mobileNumber = that.data.mobile
        if (action === 'confirm') {
          this.onConfirm()
          var dialogConfirm = this.data.dialogConfirm
          if (dialogConfirm === 0) {
            resolve(false)
          } else {
            resolve(true)
          }
        } else {
          // 拦截取消操作
          this.onClose()
          resolve(false)
        }
      }, 500)
    })
  }
})